/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hyjiacan.deadqq.client.window;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

import org.hyjiacan.deadqq.client.i18n.I18N;
import org.hyjiacan.deadqq.client.window.event.Login;
import org.hyjiacan.deadqq.common.entity.User;
import org.hyjiacan.deadqq.common.util.WindowUtil;


/**
 * 登录窗口，程序启动时直接看到的窗口，用户输入账号和密码进行登录。该窗口提供注册 新用户和找回密码的接口
 *
 * @author hyjiacan
 */
public class LoginWindow extends JFrame implements Runnable {

    /**
	 * 
	 */
	private static final long serialVersionUID = -2941613309666782912L;
	/**
     * Creates new form LoginWindow
     */
    public LoginWindow() {
    }

    @Override
    public void run() {
        initComponents();
        login.addActionListener(new Login());
        WindowUtil.center(this);
        this.setVisible(true);
        WindowManager.put(this.getClass().getName(), this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        accountlb = new JLabel();
        account = new JTextField();
        pswdlb = new JLabel();
        pswd = new JPasswordField();
        registNewAccount = new JLabel();
        forgetPassword = new JLabel();
        login = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        accountlb.setText(I18N.getString("ACCOUNT")); // NOI18N

        pswdlb.setText(I18N.getString("PASSWORD")); // NOI18N

        registNewAccount.setForeground(new Color(0, 51, 255));
        System.out.println(I18N.getString("SIGN UP"));
        registNewAccount.setText(I18N.getString("SIGN UP")); // NOI18N
        registNewAccount.setToolTipText(I18N.getString("SIGN UP A NEW ACCOUNT")); // NOI18N
        registNewAccount.setCursor(new Cursor(Cursor.HAND_CURSOR));
        registNewAccount.setName(I18N.getString("REGIST NEW ACCOUNT")); // NOI18N
        registNewAccount.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                registNewAccountMouseClicked(evt);
            }
        });

        forgetPassword.setForeground(new Color(0, 51, 255));
        forgetPassword.setText(I18N.getString("FORGET")); // NOI18N
        forgetPassword.setToolTipText(I18N.getString("FORGET PASSWORD TIP")); // NOI18N
        forgetPassword.setCursor(new Cursor(Cursor.HAND_CURSOR));
        forgetPassword.setName(I18N.getString("FORGET PASSWORD")); // NOI18N

        login.setText(I18N.getString("LOGIN")); // NOI18N

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(login, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE))
                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(pswdlb, GroupLayout.Alignment.TRAILING)
                            .addComponent(accountlb, GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addComponent(account, GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                            .addComponent(pswd))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(registNewAccount)
                            .addComponent(forgetPassword))))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(accountlb)
                    .addComponent(account, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(registNewAccount))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(pswdlb)
                    .addComponent(pswd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(forgetPassword))
                .addGap(35, 35, 35)
                .addComponent(login, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void registNewAccountMouseClicked(MouseEvent evt) {//GEN-FIRST:event_registNewAccountMouseClicked
        new Thread(new RegistWindow(this)).start();
        this.setVisible(false);
    }//GEN-LAST:event_registNewAccountMouseClicked

    /**
     * 获取用户输入的数据，将其构造成一个User对象，然后返回User
     *
     * @return 用户输入的数据构造成的User对象
     * @throws Exception 用户输入的不是数字
     */
    public User getUser() throws Exception {
        String number = account.getText();
        String password = String.valueOf(pswd.getPassword());
        // if (number.isEmpty() && number.matches("\\d+") &&
        // !password.isEmpty()) {
        if (!number.isEmpty() || number.matches("\\d+") || !password.isEmpty()) {
            return new User(Long.valueOf(number), password);
        }
        throw new Exception("账号或密码输入错误");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JTextField account;
    private JLabel accountlb;
    private JLabel forgetPassword;
    private JButton login;
    private JPasswordField pswd;
    private JLabel pswdlb;
    private JLabel registNewAccount;
    // End of variables declaration//GEN-END:variables
}
